package pers.youqu.item.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pers.youqu.item.dto.SpecGroupDTO;
import pers.youqu.item.dto.SpecParamDTO;
import pers.youqu.item.po.SpecParam;
import pers.youqu.item.service.SpecGroupService;
import pers.youqu.item.service.SpecParamService;

import java.util.List;

@RestController
public class SpecController {

    private SpecGroupService groupService;
    private SpecParamService paramService;

    public SpecController(SpecGroupService groupService, SpecParamService paramService) {
        this.groupService = groupService;
        this.paramService = paramService;
    }

    /**
     * 根据商品分类id，查询规格组的集合
     * @param cId 分类id
     * @return 规格组集合
     */
    @GetMapping("/spec/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> findSpecGroupByBrandId(@RequestParam("id") Long cId) {
        return ResponseEntity.ok(groupService.findSpecGroupByBrandId(cId));
    }

    /**
     * 根据条件查询规格参数集合
     * @param cId 分类id
     * @param gId 组id
     * @param searching 是否可搜索
     * @return 参数信息
     */
    @GetMapping("/spec/params")
    public ResponseEntity<List<SpecParamDTO>> findSpecParam(
            @RequestParam(value = "categoryId",required = false) Long cId,
            @RequestParam(value = "groupId",required = false) Long gId,
            @RequestParam(value = "searching",required = false) Boolean searching
    ) {
        return ResponseEntity.ok(paramService.findSpecParam(cId, gId, searching));
    }

    /**
     * 根据分类id查询规格组及组内参数
     * @param cid 分类id
     * @return
     */
    @GetMapping("/spec/list")
    public ResponseEntity<List<SpecGroupDTO>> findSpecGroupListByCid(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(groupService.findSpecGroupListByCid(cid));
    }

    /**
     * 新增规格组
     * @param specGroupDTO 新增规格组的属性值
     */
    @PostMapping("/spec/group")
    public ResponseEntity<Void> addSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        groupService.addSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组
     * @param specGroupDTO 新增规格组的属性值
     */
    @PutMapping("/spec/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        groupService.updateSpecGroup(specGroupDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 新增规格参数
     * @param specParamDTO 新增规格组的属性值
     */
    @PostMapping("/spec/param")
    public ResponseEntity<Void> addSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        paramService.addSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数
     * @param specParamDTO 新增规格组的属性值
     */
    @PutMapping("/spec/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        paramService.updateSpecParam(specParamDTO);
        return ResponseEntity.ok().build();
    }
}